package com.huaji.learn.ui.view;

import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huaji.learn.R;
import com.huaji.learn.model.AssociativeMemoryModel;
import com.huaji.learn.model.CornellModel;
import com.huaji.learn.model.EbbinghausModel;
import com.huaji.learn.model.FeynmanModel;
import com.huaji.learn.model.NoteModel;
import com.huaji.learn.repository.WeatherRepository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 笔记详情的控件
 */
public class NoteDetailView extends LinearLayout {
    /**
     * 时间文本
     */
    private TextView txtTime;
    /**
     * 天气文本
     */
    private TextView txtWeather;
    /**
     * 勾选框
     */
    private CheckBox chkRemind;
    private CheckBox chkRepeat;
    private CheckBox chkRethink;
    /**
     * 费曼感受
     */
    private EditText edtFeeling;
    /**
     * 费曼次数
     */
    private TextView txtFCount;
    /**
     * 费曼复习次数
     */
    private TextView txtRCount;
    //康奈尔的四个输入框
    private EditText edtKReduce;
    private EditText edtKRecite;
    private EditText edtKRecord;
    private EditText edtKReflect;
    //联想记忆法输入框
    private EditText edtMemory;
    //艾宾浩斯曲线的6个复选框
    private CheckBox chkE1;
    private CheckBox chkE3;
    private CheckBox chkE5;
    private CheckBox chkE14;
    private CheckBox chkE21;
    private CheckBox chkE30;

    private Handler handler;

    // 不同参数的构造方法
    public NoteDetailView(Context context) {
        super(context);
        init(context);
    }

    public NoteDetailView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public NoteDetailView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化控件
     * @param context 上下文
     */
    private void init(Context context) {
        // 使用布局文件将子控件添加到自定义控件中
        LayoutInflater.from(context).inflate(R.layout.note_detail_view, this, true);

        // 查找控件
        this.findView();
        // 绑定事件
        this.setEvent();

        //用于 定时任务
        handler = new Handler();
        // 启动定时任务，每隔一秒更新一次时间
        startRepeatingTask();
    }

    void findView(){
        this.txtWeather = findViewById(R.id.weatherTextView);
        this.txtTime = findViewById(R.id.timeTextView);
        this.chkRemind =findViewById(R.id.checkFeynmanRemind);
        this.chkRethink = findViewById(R.id.checkFeynmanRethink);
        this.chkRepeat = findViewById(R.id.checkFeynmanRepeat);
        this.txtRCount = findViewById(R.id.textViewFeynmanRCount);
        this.txtFCount = findViewById(R.id.textViewFeynmanFCount);
        this.edtKRecord = findViewById(R.id.editTextKRecord);
        this.edtKReflect = findViewById(R.id.editTextKReflect);
        this.edtKRecite = findViewById(R.id.editTextKRecite);
        this.edtKReduce = findViewById(R.id.editTextKReduce);
        this.edtMemory = findViewById(R.id.editTextMemory);
        this.chkE1 = findViewById(R.id.checkE1);
        this.chkE3 = findViewById(R.id.checkE3);
        this.chkE5 = findViewById(R.id.checkE5);
        this.chkE14 = findViewById(R.id.checkE14);
        this.chkE21 = findViewById(R.id.checkE21);
        this.chkE30 = findViewById(R.id.checkE30);
        this.edtFeeling = findViewById(R.id.editTextFeynmanFeeling);
    }

    void setEvent() {
        // 费曼次数的几个加减按钮
        Button btnFCountAdd = findViewById(R.id.btnFeynmanFCountAdd);
        Button btnFCountSub = findViewById(R.id.btnFeynmanFCountSub);
        Button btnRCountAdd = findViewById(R.id.btnFeynmanRCountAdd);
        Button btnRCountSub = findViewById(R.id.btnFeynmanRCountSub);

        btnRCountAdd.setOnClickListener(v -> {
            String c = txtRCount.getText().toString();
            int count = Integer.parseInt(c);
            txtRCount.setText(count + 1 + "");
        });

        btnRCountSub.setOnClickListener(v -> {
            int count = Integer.parseInt(txtRCount.getText().toString());
            txtRCount.setText(count - 1 + "");
        });

        btnFCountAdd.setOnClickListener(v -> {
            int count = Integer.parseInt(txtFCount.getText().toString());
            txtFCount.setText(count + 1 + "");
        });

        btnFCountSub.setOnClickListener(v -> {
            int count = Integer.parseInt(txtFCount.getText().toString());
            txtFCount.setText(count - 1 + "");
        });
    }

    /**
     * 对控件赋值
     * @param note 要赋值的笔记对象
     */
    public void setNote(NoteModel note) {
        if (note == null) {
            return;
        }

        if (note.getId() == -1) {
            findViewById(R.id.btnDeleteNote).setVisibility(INVISIBLE);
            note.setCreateWeather(WeatherRepository.getCurrentWeather("Beijing"));
        } else {
            findViewById(R.id.btnDeleteNote).setVisibility(VISIBLE);
        }

        txtWeather.setText(note.getCreateWeather());
        chkRemind.setChecked(note.getFeynman().isRemind());
        chkRepeat.setChecked(note.getFeynman().isRepeat());
        chkRethink.setChecked(note.getFeynman().isRethink());
        edtFeeling.setText(note.getFeynman().getFeelings());
        txtFCount.setText(note.getFeynman().getFeynmanCount() + "");
        txtRCount.setText(note.getFeynman().getReviewCount() + "");

        edtKReduce.setText(note.getCornell().getReduce());
        edtKRecite.setText(note.getCornell().getRecite());
        edtKReflect.setText(note.getCornell().getReflect());
        edtKRecord.setText(note.getCornell().getRecord());

        edtMemory.setText(note.getAssociativeMemory().getMemory());

        List<Integer> list = note.getEbbinghaus().getReviewDayList();
        chkE1.setChecked(list.contains(1));
        chkE3.setChecked(list.contains(3));
        chkE5.setChecked(list.contains(5));
        chkE14.setChecked(list.contains(14));
        chkE21.setChecked(list.contains(21));
        chkE30.setChecked(list.contains(30));
    }

    /**
     * 获取控件的值
     * @return 获取的值
     */
    public NoteModel getNote() {
        NoteModel note = new NoteModel();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = dateFormat.format(calendar.getTime());
        note.setCreateTime(currentTime);
        note.setCreateWeather(WeatherRepository.getCurrentWeather("Beijing"));

        FeynmanModel feynmanModel = new FeynmanModel();
        feynmanModel.setFeynmanCount(Integer.parseInt(txtFCount.getText().toString()));
        feynmanModel.setReviewCount(Integer.parseInt(txtRCount.getText().toString()));
        feynmanModel.setRemind(chkRemind.isChecked());
        feynmanModel.setRepeat(chkRepeat.isChecked());
        feynmanModel.setRethink(chkRethink.isChecked());
        String str = edtFeeling.getText().toString();
        feynmanModel.setFeelings(str);
        note.setFeynman(feynmanModel);

        CornellModel cornellModel = new CornellModel();
        cornellModel.setReduce(edtKReduce.getText().toString());
        cornellModel.setRecord(edtKRecord.getText().toString());
        cornellModel.setReflect(edtKReflect.getText().toString());
        cornellModel.setRecite(edtKRecite.getText().toString());
        note.setCornell(cornellModel);

        AssociativeMemoryModel associativeMemoryModel = new AssociativeMemoryModel();
        associativeMemoryModel.setMemory(edtMemory.getText().toString());
        note.setAssociativeMemory(associativeMemoryModel);

        List<Integer> list = new ArrayList<>();
        if (chkE1.isChecked()) {
            list.add(1);
        }
        if (chkE3.isChecked()) {
            list.add(3);
        }
        if (chkE5.isChecked()) {
            list.add(5);
        }
        if (chkE14.isChecked()) {
            list.add(14);
        }
        if (chkE21.isChecked()) {
            list.add(21);
        }
        if (chkE30.isChecked()) {
            list.add(30);
        }
        EbbinghausModel ebbinghausModel = new EbbinghausModel();
        ebbinghausModel.setReviewDayList(list);
        note.setEbbinghaus(ebbinghausModel);

        return note;
    }

    public void saveButtonOnClick(OnClickListener listener) {
        Button btnSave = findViewById(R.id.btnSaveNote);
        btnSave.setOnClickListener(listener);
    }

    public void deleteButtonOnClick(OnClickListener listener) {
        Button btnDel = findViewById(R.id.btnDeleteNote);
        btnDel.setOnClickListener(listener);
    }

    private final Runnable updateTimeRunnable = new Runnable() {
        @Override
        public void run() {
            updateCurrentTime();
            handler.postDelayed(this, 1000); // 间隔1秒后再次执行
        }
    };

    // 启动定时任务
    private void startRepeatingTask() {
        updateTimeRunnable.run();
    }

    // 停止定时任务
    private void stopRepeatingTask() {
        handler.removeCallbacks(updateTimeRunnable);
    }

    // 更新当前时间
    private void updateCurrentTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        String currentTime = dateFormat.format(new Date());
        txtTime.setText(currentTime);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        // 在这里执行与Activity或Fragment的生命周期相关的清理操作
        // 停止定时任务
        stopRepeatingTask();
    }
}
